package com.qianduo.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.qianduo.dao.IAccountDao;
import com.qianduo.exception.DaoException;
import com.qianduo.po.QianduoAccount;
import com.qianduo.po.QianduoAccountView;
import com.qianduo.util.HibernateUtil;
import common.page.PageList;
import common.page.PaginationQuery;
import common.util.StringUtil;

@SuppressWarnings("finally")
public class AccountDaoImpl extends HibernateUtil implements IAccountDao {
	public void saveOrUpdateAccount(QianduoAccount account) throws DaoException{
		try{
			getHibernateTemplate().merge(account);
		}catch(Exception e){
			throw new DaoException(className+" saveOrUpdateAccount",e);
		}finally{
		}
	}
	public QianduoAccountView getQianduoAccount(String username) throws DaoException {
		QianduoAccountView ret	= null;
		try{
			Criteria ca	= getSessionFactory().getCurrentSession().createCriteria(QianduoAccountView.class);
			ca.add(Restrictions.eq("username", username));
			Object obj	= ca.uniqueResult();
			if(null != obj){
				ret		= (QianduoAccountView)obj;
			}
		}catch(Exception e){
			throw new DaoException(className+" getQianduoAccount",e);
		}finally{
			return ret;
		}
	}
	public QianduoAccountView getQianduoAccount(String username, String password) throws DaoException {
		QianduoAccountView ret	= null;
		try{
			Criteria ca	= getSessionFactory().getCurrentSession().createCriteria(QianduoAccountView.class);
			ca.add(Restrictions.eq("username", username));
			ca.add(Restrictions.eq("password", password));
			Object obj	= ca.uniqueResult();
			if(null != obj){
				ret		= (QianduoAccountView)obj;
			}
		}catch(Exception e){
			throw new DaoException(className+" getQianduoAccount",e);
		}finally{
			return ret;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<QianduoAccountView>	listAccount(String username,Integer accountType,Integer agencyID,String agencyName,String ext,String phone,Integer isVerify,Integer isCert,Integer isBusinessLicense,Integer recommendStar,Integer cityID,String companyName,String productType,String address,PageList pageList) throws DaoException{
		List<QianduoAccountView> retList	= null;
		try{
			Criteria ca	= getSessionFactory().getCurrentSession().createCriteria(QianduoAccountView.class);
			if(null != agencyID){
				ca.add(Restrictions.eq("id", agencyID));
			}
			if(!StringUtil.isEmpty(username)){
				ca.add(Restrictions.like("username", "%"+username+"%"));
			}
			if(null != accountType){
				ca.add(Restrictions.eq("type", accountType));
			}
			if(!StringUtil.isEmpty(agencyName)){
				ca.add(Restrictions.like("name", "%"+agencyName+"%"));
			}
			if(!StringUtil.isEmpty(ext)){
				ca.add(Restrictions.like("ext", "%"+ext+"%"));
			}
			if(!StringUtil.isEmpty(phone)){
				ca.add(Restrictions.like("phone", "%"+phone+"%"));
			}
			if(null != isVerify){
				ca.add(Restrictions.eq("isVerify", isVerify));
			}
			if(null != isCert){
				ca.add(Restrictions.eq("isCert", isCert));
			}
			if(null != isBusinessLicense){
				ca.add(Restrictions.eq("isBusinessLicense", isBusinessLicense));
			}			
			if(null != cityID){
				ca.add(Restrictions.eq("cityID", cityID));
			}
			if(!StringUtil.isEmpty(companyName)){
				ca.add(Restrictions.like("companyName", "%"+companyName+"%"));
			}
			if(!StringUtil.isEmpty(productType)){
				ca.add(Restrictions.like("products", "%|"+productType+"|%"));
			}			
			if(!StringUtil.isEmpty(address)){
				ca.add(Restrictions.like("address", "%"+productType+"%"));
			}			
			if(null != pageList){				
				retList	= PaginationQuery.queryCriteria(ca, pageList);
			}else{
				retList	= ca.list();
			}
		}catch(Exception e){
			throw new DaoException(className+" listAccount",e);
		}finally{
			return retList;
		}
	}
}
